/*
 * mysqlInstance.h
 *
 *  Created on: 05.04.2009
 *      Author: Christian Stupp
 */

#ifndef MYSQLINSTANCE_H_
#define MYSQLINSTANCE_H_

#include "object.h"
#ifdef _WIN32
#include <winsock.h>
#include <io.h>
/* Unix-System */
#endif
#include <mysql/mysql.h>
#include <vector>
#include "debuger.h"
#include "array.h"

using namespace std;
namespace oal{
	class mysqlInstance: public object {
	private:
		static mysqlInstance* instance;
		static MYSQL *currentConnection;

		static std::map<std::string, array > tableData;		mysqlInstance();
		std::string error;
		vector<int> currentPositionVector;
		vector< object* > currentItemVector;
		vector< object* > currentItems;
		std::string database;
	public:
		static mysqlInstance *makeInstance(std::string path = "");
		static void eraseInstance(void);
		static void setInstance(mysqlInstance *newObject);

		MYSQL *currentObjectPointer;
		virtual ~mysqlInstance();

		bool connect(std::string host,string username,string password, string database="", int port=3306, string socket="");
		array getFieldsFromTable(std::string table);
		int query(std::string query);
		std::string getError(void);
		std::string getDatabase(void);

		object *mysqlFetchAssoc(void);
		string buildQueryStringFromMap(map<string,string> value);
		string lastInsertId(void);
		void freeResult(void);
	};

	bool mysql_connect(std::string host,string username,string password, string database, int port=0, string socket="");
	void mysql_close(void);
	int mysql_query(std::string query);
	oal::array mysql_fetch_fields(std::string table);
	std::string mysql_error(void);
	object *mysql_fetch_assoc(void);
	string mysql_insert_query(std::string table, std::map<std::string,std::string> value);
	void mysql_update_query(std::string table,std::string where, std::map<std::string,std::string> value);
	void mysql_free_result(void);
};

#endif /* MYSQLINSTANCE_H_ */
